/*
 * @lc app=leetcode.cn id=495 lang=cpp
 *
 * [495] 提莫攻击
 */

// @lc code=start
class Solution
{
public:
  int findPoisonedDuration(vector<int>& timeSeries, int duration)
  {
    int ans = 0, n = timeSeries.size();
    int start = timeSeries[0], end = timeSeries[0] + duration;
    for (int i = 0; i < n; ++i) {
      // 中毒结束
      if (timeSeries[i] >= end) {
        ans += end - start;
        start = timeSeries[i];
        end = timeSeries[i] + duration;
      }
      else
        end = max(end, timeSeries[i] + duration);
    }
    ans += end - start;
    return ans;
  }
};
// @lc code=end
